package com.zhongc.advance.api.impl;

import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import com.zhongc.advance.api.UserService;
import com.zhongc.advance.common.CommonCode;
import com.zhongc.advance.common.enums.CommonErrorCode;
import com.zhongc.advance.common.result.Result;
import com.zhongc.advance.common.result.Results;
import com.zhongc.advance.common.result.Status;
import com.zhongc.advance.common.util.BeanUtils;
import com.zhongc.advance.common.util.Validator;
import com.zhongc.advance.info.User;
import com.zhongc.advance.order.LoginOrder;
import com.zhongc.advance.order.UserOrder;
import com.zhongc.advance.service.AdvanceUserService;

@Service("userService")
public class UserServiceImpl implements UserService {
    private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private AdvanceUserService advanceUserService;

    @Override
    public Result register(UserOrder order) {
        logger.info("注册会员入参  UserOrder ->{}", order);
        Result result = new Result();
        try {
            Validator.validate(order);
        } catch (Exception e) {
            result.setCode(CommonCode.FAIL);
            result.setDescription(e.getMessage());
            result.setStatus(Status.FAIL);
            logger.info("注册会员出参  Results->{}", result);
            return result;
        }
        User user = BeanUtils.copyProperties(order, User.class);
        User us = advanceUserService.queryUserByUsername(order.getUserName());
        if (us != null) {
            logger.info("注册会员失败失败！原因：用户名已经存在");
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription(CommonErrorCode.DB_ERROR.getMessage());
            logger.info("注册会员出参  Results->{}", result);
            return result;
        }
        try {
            advanceUserService.register(user);
            logger.error("注册会员注册成功");
            result.setCode(CommonCode.SUCCESS);
            result.setStatus(Status.SUCCESS);
            result.setDescription(CommonErrorCode.SUCCESS.getMessage());
        }
        catch (DataAccessException e) {
            logger.error("注册会员失败失败！原因：{}", e);
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription(CommonErrorCode.DB_ERROR.getMessage());
        } catch (Exception e) {
            logger.error("注册会员失败失败！原因：{}", e);
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription(CommonErrorCode.UNKNOWN_ERROR.getMessage());
        }
        logger.info("注册会员出参  Results->{}", result);
        return result;
    }

	@Override
	public Result login(LoginOrder order) {
		logger.info("会员登陆入参  LoginOrder->{}", order);
		Result result = new Result();
		try {
			User us = advanceUserService.login(order);
			if (us==null) {
				logger.error("会员登录失败");
				result.setCode(CommonCode.FAIL);
				result.setStatus(Status.FAIL);
				result.setDescription(CommonErrorCode.USERNAME_OR_PASSWORD_ERROR.message());
				result.setResult(us);
				return result;
			}
			logger.info("会员登录成功");
			result.setCode(CommonCode.SUCCESS);
			result.setStatus(Status.SUCCESS);
			result.setDescription(CommonErrorCode.SUCCESS.getMessage());
			result.setResult(us);
		} catch (DataAccessException e) {
			logger.error("会员登录失败失败！原因：{}", e);
			result.setCode(CommonCode.FAIL);
			result.setStatus(Status.FAIL);
			result.setDescription(CommonErrorCode.DB_ERROR.getMessage());
		} catch (Exception e) {
			logger.error("会员登录失败失败！原因：{}", e);
			result.setCode(CommonCode.FAIL);
			result.setStatus(Status.FAIL);
			result.setDescription(CommonErrorCode.UNKNOWN_ERROR.getMessage());
		}
		logger.info("会员登录出参  Results->{}", result);
		return result;
	}

	@Override
	public Result queryUserByUserId(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Results queryUser(Map<String, Object>... maps) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryUserByUsername(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Results updateUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result updatePassword(String userId, String passWord) {
		// TODO Auto-generated method stub
		return null;
	}



}
